/*
 * SASPManagerApp.java
 */

package saspmanager;

import java.security.NoSuchAlgorithmException;
import java.util.EventObject;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.NoSuchPaddingException;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;
import java.io.*;
import org.boehn.kmlframework.kml.KmlException;
import org.jdesktop.application.Application.ExitListener;
import sasp_messages.MessageHandler;
import sasp_protocol.Protocol;
import sasp_udp.UDPTransceiver;
import sasp_protocol.Entity;
import sasp_KML.Self;
import saspmanager.SASPManagerView;
/**
 * The main class of the application.
 */
public class SASPManagerApp extends SingleFrameApplication {

    private static int arg;
    private static Protocol protocol;
    private static SASPManagerView Window;

    /**
     * At startup create and show the main frame of the application.
     */
    @Override protected void startup() {
        try {
            UDPTransceiver udpTrans = new UDPTransceiver(arg);
            MessageHandler messHandle = new MessageHandler(udpTrans);
            udpTrans.addUdpListener(messHandle);
            protocol = new Protocol(messHandle);
            Runnable you = new Self(protocol);
            Thread myThread = new Thread(you);
            protocol.defself((Entity) you);
            messHandle.addMessageListener(protocol);
            udpTrans.initCipher();
            sasp_KML.KMLgen map = new sasp_KML.KMLgen(protocol, (Entity) you);
            udpTrans.addKML(map);
            protocol.addKML(map);
            myThread.start();
            Window = new SASPManagerView(this, protocol);
            map.setview(Window);
            show(Window);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(SASPManagerApp.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchPaddingException ex) {
            Logger.getLogger(SASPManagerApp.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(SASPManagerApp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void addLine(String line)
    {
        if(Window != null)
        {
            Window.txtStatus.append(line + "\n");
            Window.txtStatus.setCaretPosition(Window.txtStatus.getDocument().getLength());
        }else{
            System.out.println(line);
        }
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of SASPManagerApp
     */
    public static SASPManagerApp getApplication() {
        return Application.getInstance(SASPManagerApp.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) throws IOException, KmlException {
        /* First Arugment == Port Number of UDP Receiver */
        if( args.length == 1 ) {
            SASPManagerApp.arg = Integer.parseInt(args[0]);
        } else {
            SASPManagerApp.arg = 12345;
        }
        launch(SASPManagerApp.class, args);
    }

    @Override
    protected void shutdown() {
        // The default shutdown saves session window state.
        super.shutdown();
        protocol.disconnect();
    }
}
